package com.example.starttrekking;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class DetalhesTrekActivity extends Activity {
	private DatabaseHelper myDbHelper;
	private TextView titulo;
	public final static String TREK_TITULO = "com.example.starttrekking.TREK_TITULO";
	public final static String TREK_ID = "com.example.starttrekking.TREK_ID";
	private int trek_id;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
	    trek_id = intent.getIntExtra(EncontreUmaTrekFragment.TREK_ID,0);
	    setContentView(R.layout.activity_detalhe_trek);
	    Trek trek_escolhida = null;
	    
	    myDbHelper = DatabaseHelper.getInstance(this);
		try {
			myDbHelper.createDatabase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}
		try {
			myDbHelper.openDatabase();
		}catch(SQLException sqle){
			throw sqle;
		}
	    trek_escolhida = myDbHelper.getDetalhesTrek(trek_id);
		
		
	    titulo = (TextView) findViewById(R.id.titulo_trek);
	    titulo.setText(trek_escolhida.getNome());
	    
	    TextView comprimento= (TextView) findViewById(R.id.comprimento);
	    comprimento.setText(trek_escolhida.getComprimento());
	    
	    TextView cidade= (TextView) findViewById(R.id.cidade);
	    cidade.setText(trek_escolhida.getCidade());
	    
	    TextView estado= (TextView) findViewById(R.id.estado);
	    estado.setText(trek_escolhida.getEstado());
	    

	    findViewById(R.id.bt_viewtrek).setOnClickListener(new View.OnClickListener() {
	    	public void onClick(View v) {
	    		Intent i = new Intent(getBaseContext(), MapaTrekActivity.class);
	    		i.putExtra(TREK_TITULO, titulo.getText());
	    		i.putExtra(TREK_ID, trek_id);
	    		startActivity(i);
	    	} 
	    });

	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		myDbHelper.close();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detalhe_trek, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		/*if (id == R.id.action_settings) {
			return true;
		}*/
		return super.onOptionsItemSelected(item);
	}
}
